/**
 * 
 */
package fr.sportcom.sportcom.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import com.google.android.gms.maps.model.LatLng;


import android.location.Address;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author NeoXy
 *
 */
public class CEvent implements Parcelable {

	private static final DateFormat dFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);

	private int id;
	private Date begin;
	private Date end;
	private Location locat = new Location("");
	private Address address;
	private CSport sport;
	private int nbparticipants;
	private int idOwner;
	private boolean isPrivate;
	private String description;
	private int invite = -1;


	/**
	 * @param id
	 * @param begin
	 * @param end
	 * @param locat
	 * @param sport
	 * @param nbparticipants
	 */
	public CEvent(int id, String begin, String end, double latitude, double longitude, Address add,
			CSport sport, int nbparticipants, int idOwner, boolean isPrivate, String description, int invite) {
		this.id = id;
		this.setBegin(begin);
		if(end != null) {
			this.setEnd(end);
		}
		else {
			this.setEnd(begin);
		}
		this.setLocat(latitude, longitude);
		this.address = add;
		this.sport = sport;
		this.nbparticipants = nbparticipants;
		this.idOwner = idOwner;
		this.isPrivate = isPrivate;
		this.description = description;
		this.invite = invite;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the begin
	 */
	public Date getBegin() {
		return begin;
	}

	/**
	 * @return the begin
	 */
	public String getStrBegin() {
		return dFm.format(begin);
	}

	/**
	 * @param begin the begin to set
	 */
	public void setBegin(Date begin) {
		this.begin = begin;
	}

	/**
	 * @param begin the begin to set
	 */
	public void setBegin(String begin) {
		try {
			this.begin = dFm.parse(begin);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the end
	 */
	public Date getEnd() {
		return end;
	}

	/**
	 * @return the end
	 */
	public String getStrEnd() {
		return dFm.format(end);
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(Date end) {
		this.end = end;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(String end) {
		try {
			this.end = dFm.parse(end);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the locat
	 */
	public Location getLocat() {
		return locat;
	}

	/**
	 * @param locat the locat to set
	 */
	public void setLocat(Location locat) {
		this.locat = locat;
	}

	/**
	 * @param locat the locat to set
	 */
	public void setLocat(double latitude, double longitude) {
		this.locat.setLongitude(longitude);
		this.locat.setLatitude(latitude);
	}

	/**
	 * @return the sport
	 */
	public CSport getSport() {
		return sport;
	}

	/**
	 * @param sport the sport to set
	 */
	public void setSport(CSport sport) {
		this.sport = sport;
	}

	/**
	 * @return the nbparticipants
	 */
	public int getNbparticipants() {
		return nbparticipants;
	}

	/**
	 * @param nbparticipants the nbparticipants to set
	 */
	public void setNbparticipants(int nbparticipants) {
		this.nbparticipants = nbparticipants;
	}

	/**
	 * @return the isPrivate
	 */
	public boolean isPrivate() {
		return this.isPrivate;
	}

	/**
	 * @param isPrivate the isPrivate to set
	 */
	public void setPrivate(boolean privateEvent) {
		this.isPrivate = privateEvent;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return Dur� de l'�venement en minutes
	 */
	public long getDuration() {		
		long timeMs = end.getTime() - begin.getTime();
		if(timeMs <= 0) {
			return 0;
		}
		long min = TimeUnit.MILLISECONDS.toMinutes(timeMs);
		long sec = TimeUnit.MILLISECONDS.toSeconds(timeMs) - TimeUnit.MINUTES.toSeconds(min);
		if(sec >= 30) {
			min++;
		}
		return min;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(this.id);
		dest.writeString(this.getStrBegin());
		dest.writeString(this.getStrEnd());
		dest.writeDouble(this.locat.getLatitude());
		dest.writeDouble(this.locat.getLongitude());
		dest.writeParcelable(this.address, flags);
		dest.writeInt(this.sport.getId());
		dest.writeString(this.sport.getName());
		dest.writeInt(this.nbparticipants);
		dest.writeInt(this.idOwner);
		dest.writeInt(this.isPrivate ? 1 : 0);
		dest.writeString(this.description);
		dest.writeInt(this.invite);
	}

	public static final Parcelable.Creator<CEvent> CREATOR = new Parcelable.Creator<CEvent>() {
		@Override
		public CEvent createFromParcel(Parcel source)
		{
			return new CEvent(source);
		}

		@Override
		public CEvent[] newArray(int size)
		{
			return new CEvent[size];
		}
	};

	public CEvent(Parcel in) {
		this.id = in.readInt();
		this.setBegin(in.readString());
		this.setEnd(in.readString());
		this.setLocat(in.readDouble(), in.readDouble());
		this.address = in.readParcelable(Address.class.getClassLoader());
		this.sport = new CSport(in.readInt(), in.readString());
		this.nbparticipants = in.readInt();
		this.idOwner = in.readInt();
		this.isPrivate = in.readInt() == 1;
		this.description = in.readString();
		this.invite = in.readInt();
	}

	/**
	 * @return the idOwner
	 */
	public int getIdOwner() {
		return idOwner;
	}

	/**
	 * @param idOwner the idOwner to set
	 */
	public void setIdOwner(int idOwner) {
		this.idOwner = idOwner;
	}

	/**
	 * @return the address
	 */
	public Address getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(Address address) {
		this.address = address;
	}
	
	public int getDate(String sf) {
		/*DateFormat format = new SimpleDateFormat("yyyy", Locale.FRANCE);
		year =  Integer.parseInt(format.format(begin));
		format = new SimpleDateFormat("MM", Locale.FRANCE);
		month =  Integer.parseInt(format.format(begin));
		format = new SimpleDateFormat("dd", Locale.FRANCE);
		day =  Integer.parseInt(format.format(begin));*/
		DateFormat format = new SimpleDateFormat(sf, Locale.FRANCE);
		return Integer.parseInt(format.format(begin));
	}
	
	public String getStrBHour() {
		DateFormat format = new SimpleDateFormat("HH:mm", Locale.FRANCE);
		return format.format(begin);
	}
	
	public int getBHour(String sf) {
		/*DateFormat format = new SimpleDateFormat("HH", Locale.FRANCE);
		hour =  Integer.parseInt(format.format(begin));
		format = new SimpleDateFormat("mm", Locale.FRANCE);
		minute =  Integer.parseInt(format.format(begin));*/
		DateFormat format = new SimpleDateFormat(sf, Locale.FRANCE);
		return Integer.parseInt(format.format(begin));
	}
	
	public int getEHour(String sf) {
		/*DateFormat format = new SimpleDateFormat("HH", Locale.FRANCE);
		hour =  Integer.parseInt(format.format(end));
		format = new SimpleDateFormat("mm", Locale.FRANCE);
		minute =  Integer.parseInt(format.format(end));*/
		DateFormat format = new SimpleDateFormat(sf, Locale.FRANCE);
		return Integer.parseInt(format.format(end));
	}
	
	public LatLng getLatLngLoc() {
		return new LatLng(locat.getLatitude(), locat.getLongitude());
	}
	
	public String getStrAddress() {
		if(this.address == null) {
			return "-";
		}
		return this.address.getAddressLine(0) + "\r\n"
				+ this.address.getPostalCode() + " "
				+ this.address.getLocality();
	}
	
	public String getLocality() {
		if(this.address == null) {
			return "-";
		}
		return this.address.getLocality();
	}
	
	public static int countPart(ArrayList<User> lUser) {
		int i = 0;
		for (User user : lUser) {
			if(user.isAccepted()) {
				i++;
			}
		}
		return i;
	}
	
	public int getStatutInvite() {
		return invite;
	}
	
	public void setStatutInvite(int invite) {
		this.invite = invite;
	}
}
